/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.biz.sms.gen.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.nmf.site.common.config.Global;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.web.BaseController;
import com.nmf.site.common.utils.StringUtils;
import com.nmf.site.biz.sms.gen.entity.BizSms;
import com.nmf.site.biz.sms.gen.service.BizSmsService;

/**
 * 短信Controller
 * @author 北冥
 * @version 2018-03-22
 */
@Controller
@RequestMapping(value = "${adminPath}/gen/bizSms")
public class BizSmsController extends BaseController {

	@Autowired
	private BizSmsService bizSmsService;
	
	@ModelAttribute
	public BizSms get(@RequestParam(required=false) String id) {
		BizSms entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bizSmsService.get(id);
		}
		if (entity == null){
			entity = new BizSms();
		}
		return entity;
	}
	
	@RequiresPermissions("gen:bizSms:view")
	@RequestMapping(value = {"list", ""})
	public String list(BizSms bizSms, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BizSms> page = bizSmsService.findPage(new Page<BizSms>(request, response), bizSms); 
		model.addAttribute("page", page);
		return "sms/gen/bizSmsList";
	}

	@RequiresPermissions("gen:bizSms:view")
	@RequestMapping(value = "form")
	public String form(BizSms bizSms, Model model) {
		model.addAttribute("bizSms", bizSms);
		return "sms/gen/bizSmsForm";
	}

	@RequiresPermissions("gen:bizSms:edit")
	@RequestMapping(value = "save")
	public String save(BizSms bizSms, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bizSms)){
			return form(bizSms, model);
		}
		bizSmsService.save(bizSms);
		addMessage(redirectAttributes, "保存短信成功");
		return "redirect:"+Global.getAdminPath()+"/gen/bizSms/?repage";
	}
	
	@RequiresPermissions("gen:bizSms:edit")
	@RequestMapping(value = "delete")
	public String delete(BizSms bizSms, RedirectAttributes redirectAttributes) {
		bizSmsService.delete(bizSms);
		addMessage(redirectAttributes, "删除短信成功");
		return "redirect:"+Global.getAdminPath()+"/gen/bizSms/?repage";
	}

}